/**
 * Copyright (C), 2011-2017
 * File Name: AdCacheKeyBuilder.java
 * Encoding: UTF-8
 * Date: 18-1-25 上午11:16
 * History:
 */
package com.swak.frame.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.google.common.base.Joiner;


/**
 * @FileName  CacheBuilder.java
 * @author colley
 * @version 1.0  Date: 18-1-25 上午11:16
 */
public class CachesBuilder implements java.io.Serializable {
    private static final long serialVersionUID = 6226198429940452349L;
    private String prefix;

    public CachesBuilder(String prefix) {
        this.prefix = prefix;
    }
    
    public CachesBuilder() {
        this(StringPool.BLANK);
    }

    public Builder forBilder() {
        return new Builder(prefix);
    }
    
    public Builder forBilder(String symbol) {
        return new Builder(prefix,symbol);
    }

    public static class Builder {
        private String prefix;
        private String symbol;
        private List<Object> messageParams = new ArrayList<Object>();

        public Builder(String prefix) {
            this(prefix, StringPool.UNDERLINE);
        }
        
        public Builder(String prefix,String symbol) {
            this.prefix = prefix;
            this.symbol = symbol;
        }

        public Builder append(Object cacheKey) {
            if (cacheKey != null) {
            	messageParams.add(cacheKey);
            }

            return this;
        }

        public String build() {
            StringBuilder builder = new StringBuilder(prefix);

            if (CollectionUtils.isNotEmpty(messageParams)) {
            	builder = Joiner.on(symbol).skipNulls().appendTo(builder, messageParams);
            }
            return builder.toString();
        }
    }
}
